package com.sudoku.ui.content;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import com.sudoku.common.Constants;
import com.sudoku.common.SqliteUtils;
import com.sudoku.listener.MenuExitListener;
import com.sudoku.listener.MenuNewGameListener;
import com.sudoku.listener.MenuReadHistoryListener;
import com.sudoku.listener.MenuResetListener;
import com.sudoku.ui.Sudoku;

public class SMenu extends JMenuBar {
	private static final long serialVersionUID = -5642886213263272359L;
	
	private Sudoku sudoku;
	private JMenu file;
	private JMenu history;
	
	public SMenu(Sudoku sudoku) {
		this.sudoku = sudoku;
		this.file = createFileMenu();
		this.history = createHistory();
		add(file);
		add(history);
		setVisible(true);
	}
	
	public JMenu createFileMenu() {
		JMenu menu = new JMenu("File(F)");
		menu.setMnemonic(KeyEvent.VK_F);
		
		JMenuItem item = new JMenuItem("New Game(N)", KeyEvent.VK_N);
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
		item.addActionListener(new MenuNewGameListener(sudoku));
		// 如果当前在首页，禁用新游戏选项
		if (Constants.LABEL_HOME.equals(sudoku.getAtCard())) {
			item.setEnabled(false);
		}
		
		menu.add(item);
		
		item = new JMenuItem("Reset Game(R)", KeyEvent.VK_R);
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));
		item.addActionListener(new MenuResetListener(sudoku));
		// 如果当前在首页，禁用重置选项
		if (Constants.LABEL_HOME.equals(sudoku.getAtCard())) {
			item.setEnabled(false);
		}
		menu.add(item);
		
		menu.addSeparator();
		
		item = new JMenuItem("Exit(W)", KeyEvent.VK_W);
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK));
		item.addActionListener(new MenuExitListener(sudoku));
		menu.add(item);
		
		return menu;
	}

	public JMenu createHistory() {
		JMenu menu = new JMenu("Read(H)");
		menu.setMnemonic(KeyEvent.VK_H);
		
		/**
		 * 从数据库中获得记录，选择10条
		 */
		
		List<Map<String, Object> > result = SqliteUtils.selectIdTimeAndLevel();
		
		if (result!=null && result.size()!=0) {
			final Map<Integer, String> levelMap = new HashMap<Integer, String>() {{
				put(Constants.VALUE_EASY, Constants.LABEL_EASY);
				put(Constants.VALUE_INTERMEDIATE, Constants.LABEL_INTERMEDIATE);
				put(Constants.VALUE_DIFFFICULT, Constants.LABEL_DIFFICULT);
			}};
			

			for (int i = 0; i<Constants.HISTORY_NUM && i<result.size(); i++) {
				Map<String, Object> resultItem = result.get(i);
				int id = (Integer)resultItem.get("id");
				String time = (String)resultItem.get("time");
				int level = (Integer)resultItem.get("level");
				
				JMenuItem item = new JMenuItem(String.format("%2d", i+1) + ". " + time + " " + levelMap.get(level));
				item.addActionListener(new MenuReadHistoryListener(sudoku, id));
				menu.add(item);
			}			
		} else {
			JMenuItem item = new JMenuItem("Noting Here!");
			menu.add(item);
		}
		
		return menu;
	}
}
